#
# SearchResult.py
# (c) 2008 Will Baker.
#

class SearchResult:
    """
    The result of a search in the library. This records the item found as well as other
    search data such as relevance, etc.
    """
    sort_tag = None
    
    def __init__( self, item, relevance ):
        self.item = item
        self.relevance = relevance
        
    def sort_by_relevance( a, b ):
        """
        Sort SearchResults by relevance. The comparison is reversed because we want to sort
        from highest relevance to least.
        """
        return cmp( b.relevance, a.relevance )
        
    def sort_by_tag( cls, tag ):
        """
        Sort SearchResults on a set of tags.
        """
        def _sort_by_tag( tag, a, b ):
            if tag in a.item.tags and tag in b.item.tags:
                return cmp( a.item.tags[tag].order, b.item.tags[tag].order )
            else:
                return cmp( a.item.name, b.item.name )
        
        return lambda a, b: _sort_by_tag(tag, a, b)
    sort_by_tag = classmethod( sort_by_tag )
